//
//  ViewController.m
//  12新汽车品牌
//
//  Created by apple on 15/7/31.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "ViewController.h"
#import "Car.h"
#import "CarsGroup.h"
@interface ViewController ()<UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSArray *carsGroup;
@end

@implementation ViewController


- (NSArray *)carsGroup
{
    if(_carsGroup ==nil)
    {
        _carsGroup = [CarsGroup carsToList];
    }
    return _carsGroup;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.tableView.dataSource =self;
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.carsGroup.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    CarsGroup *group = self.carsGroup[section];
    
    return group.cars.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifer = @"car";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    if (cell==nil) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifer];
    }
    CarsGroup *group = self.carsGroup[indexPath.section];
    
    cell.textLabel.text = [group.cars[indexPath.row] name];
    cell.imageView.image = [UIImage imageNamed:[group.cars[indexPath.row] icon]];
    return cell;
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    CarsGroup *group = self.carsGroup[section];
    return group.title;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return  [self.carsGroup valueForKeyPath:@"title"];
}

@end
